import { _decorator, Component, instantiate, Node, Vec3 } from 'cc';
import { GAME_PREFAB, GlobalData } from '../../../Data/GlobalData';
import { SingleEnemy } from './SingleEnemy';
import { GlobaInterface, GunInfo } from '../../../Data/GlobaInterface';
import { SinglePao } from '../MiddleLoad/SinglePao';
import { MiddleLoad } from '../MiddleLoad/MiddleLoad';
import { Tools } from '../../../Tool/Tools';
import { BaseComponent } from '../../../Manager/UIManager/BaseComponent';
const { ccclass, property } = _decorator;

@ccclass('EnemyLoad')
/**
 * 敌人层
 */
export class EnemyLoad extends BaseComponent {

    //敌人数组
    private EnemyArray: Node[] = [];

    //没有用的数组
    private NoUseArray: Node[] = [];

    //创建时间
    private CreateTime: number = 0;
    //创建间隔
    private CreateDistance: number = 0.5;

    //中间炮弹层
    @property(Node)
    private MiddleLoad: Node = null;

    //不移动的数组
    private NoMoveArr: Node[] = [];

    //检测距离
    private CheckDistance: number = 100;
    //差值
    private CheckValue: number = 10;

    //间距位置
    private SpaceIndex: number = 30;

    //创建数量
    private CreateNum: number = 5;

    //第一个创建得
    private FirstCreate: boolean = true;

    protected onLoad(): void {
        for (let path of GlobalData.MovePath) {

            //上一个数量
            let lastNum: number = GlobalData.MoveSpace.length - 1 < 0 ? 0 : GlobalData.MoveSpace[GlobalData.MoveSpace.length - 1];
            console.log(GlobalData.MoveSpace.length, lastNum)

            if (path.star.x == path.end.x) {//我是竖着的
                GlobalData.MoveSpace.push((Math.abs(path.star.y - path.end.y)) / GlobalData.MoveSpeed + lastNum);
            }

            if (path.star.y == path.end.y) {//我是横着的
                GlobalData.MoveSpace.push((Math.abs(path.star.x - path.end.x)) / GlobalData.MoveSpeed + lastNum);
            }
        }

        console.log("移动距离", GlobalData.MoveSpace)
    }

    protected OnShow(): void {
        this.FirstCreate = true;
        this.CountNum = 2 * 60;
        GlobalData.GameSpeed = 5;
        for (let enemy of this.EnemyArray) {
            this.NoUseArray.push(enemy);
            enemy.setParent(null)
            enemy.active = false;
        }

        this.EnemyArray = [];
        this.CreateTime = 0;

        for (let nomove of this.NoMoveArr) {
            this.NoUseArray.push(nomove);
            nomove.setParent(null)
            nomove.active = false;
        }
    }

    /**
     * 创建敌人
     */
    private CreateEnemy() {

        // this.CreateNum--;
        // if (this.CreateNum < 0) return;

        //每次创建的判断是否能发射炮弹
        this.MiddleLoad.getComponent(MiddleLoad).FireBullet();

        if (this.NoUseArray.length > 0) {
            let se: Node = this.NoUseArray[0]
            this.EnemyArray.push(se);
            se.active = true;
            se.setParent(this.node);
            se.setPosition(new Vec3(0, 240));
            se.setPosition(new Vec3(GlobalData.MovePath[0].star.x, GlobalData.MovePath[0].star.y));
            se.getComponent(SingleEnemy).UpdateStyle(this.FirstCreate ? 8 : Math.floor(Math.random() * GlobalData.BrickTypeNum));
            se.setSiblingIndex(0)
            this.NoUseArray.shift();
            this.FirstCreate = false;
            return;
        }

        let se: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.SINGLEENEMY]);
        this.node.addChild(se);
        se.setSiblingIndex(0)
        se.setPosition(new Vec3(0, 240));
        se.setPosition(new Vec3(GlobalData.MovePath[0].star.x, GlobalData.MovePath[0].star.y));
        se.getComponent(SingleEnemy).UpdateStyle(this.FirstCreate ? 8 : Math.floor(Math.random() * GlobalData.BrickTypeNum));
        this.FirstCreate = false;
        this.EnemyArray.push(se);

    }

    //倒计时
    private CountNum: number = 0.5 * 60;

    protected update(dt: number): void {
        if (GlobalData.GameOver) return;

        this.CountNum--;
        if (this.CountNum < 0) {
            GlobalData.GameSpeed = 1;
        }

        for (let i = 0; i < GlobalData.GameSpeed; i++) {
            this.CreateTime += dt;
            if (this.CreateTime >= this.CreateDistance) {
                this.CreateTime = 0;
                this.CreateEnemy();
            }

            for (let i = 0; i < this.EnemyArray.length; i++) {
                this.EnemyArray[i].getComponent(SingleEnemy).update0(dt);
            }

            // for (let nomove of this.NoMoveArr) {
            //     nomove.getComponent(SingleEnemy).UpdateIndex(-nomove.getComponent(SingleEnemy).MoveIndex);
            // }

            this.CheckEnemy();
        }


    }

    /**
     * 第一个敌人和不移动的敌人检测
     */
    private CheckEnemy() {
        // return;
        if (this.NoMoveArr.length > 0) {
            //     for (let i = 0; i < this.NoMoveArr.length; i++) {
            //         if (i == this.NoMoveArr.length - 1) {
            let i = this.NoMoveArr.length - 1;
            if (this.NoMoveArr[i].getComponent(SingleEnemy).MoveIndex - this.EnemyArray[0].getComponent(SingleEnemy).MoveIndex <= this.SpaceIndex) {
                // this.NoMoveArr[i].getComponent(SingleEnemy).MoveIndex = this.EnemyArray[0].getComponent(SingleEnemy).MoveIndex + this.SpaceIndex;
                this.NoMoveArr[i].getComponent(SingleEnemy).IsMove = true;
                this.EnemyArray.unshift(this.NoMoveArr[i]);
                this.NoMoveArr.splice(i, 1);
                // break

                for (let i = 1; i < this.EnemyArray.length; i++) {
                    this.EnemyArray[i].getComponent(SingleEnemy).MoveIndex = this.EnemyArray[0].getComponent(SingleEnemy).MoveIndex - i * this.SpaceIndex;
                }

            }
        }
        //     }
        // }

    }

    /**
     * 每个敌人试探
     */
    public UpdateEnemy(_pao: Node) {

        for (let i = 0; i < this.EnemyArray.length; i++) {
            if (this.EnemyArray[i].getComponent(SingleEnemy).IamFired(_pao.getComponent(SinglePao).GunInfo)
                && this.EnemyArray[i].active
                && Math.abs(this.EnemyArray[i].position.x) <= 310
                && _pao.getComponent(SinglePao).GunInfo.Num > 0) {
                this.EnemyArray[i].active = false;//先隐藏掉

                _pao.getComponent(SinglePao).IamFire();

                let y_dis: number = this.EnemyArray[i].worldPosition.y - _pao.worldPosition.y;
                let x_dis: number = this.EnemyArray[i].worldPosition.x - _pao.worldPosition.x;

                _pao.getComponent(SinglePao).UpdateAngle(
                    Math.atan2(y_dis, x_dis)
                )
                break;
            }
        }

        //消除点
        let clear_index: number = -1;

        //再回收
        for (let i = 0; i < this.EnemyArray.length; i++) {
            if (!this.EnemyArray[i].active) {
                this.EnemyArray[i].setParent(null);
                this.NoUseArray.push(this.EnemyArray[i]);
                this.EnemyArray.splice(i, 1);
                clear_index = i;
            }
        }

        for (let i = 0; i < clear_index; i++) {
            this.EnemyArray[0].getComponent(SingleEnemy).IsMove = false;
            this.NoMoveArr.push(this.EnemyArray.shift());
        }

        if (clear_index == -1) {
            //
            for (let i = 0; i < this.NoMoveArr.length; i++) {
                if (this.NoMoveArr[i].getComponent(SingleEnemy).IamFired(_pao.getComponent(SinglePao).GunInfo)
                    && this.NoMoveArr[i].active
                    && Math.abs(this.NoMoveArr[i].position.x) <= 310
                    && _pao.getComponent(SinglePao).GunInfo.Num > 0) {
                    this.NoMoveArr[i].active = false;//先隐藏掉
                    this.NoMoveArr[i].setParent(null);
                    this.NoUseArray.push(this.NoMoveArr[i]);
                    this.NoMoveArr.splice(i, 1);
                    break;
                }
            }
        }

    }
}


